package pack;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;


/***
 * 
 * 
 * @author Marcus Schroeder
 *
 */
public class IOHandler {
	
	public static String getSequenceFromInput(String sequence){
		return sequence.toLowerCase();	
	}
	
	public static String createRandomDNASeq(int length){
		String seq ="";
		
		String[] letters = {"a","c","g","t"};
		
		for(int i = 0; i < length; i++){
			
			int a = (int) Math.floor(4*Math.random());
			seq = seq+letters[a];
		}
		
		
		return seq;
	}
	
	
	public static String[] getBunchOfSequencesFromFile(String fileLocation){
		File file = new File(fileLocation);
	    StringBuffer contents = new StringBuffer();
	    BufferedReader reader = null;

	    try
	      {
	            reader = new BufferedReader(new FileReader(file));
	            String text = null;

	            // repeat until all lines is read
	            while ((text = reader.readLine()) != null)
	            {
	            		contents.append(text)
	                    		.append(System.getProperty(
	                    			"line.separator"));	            	
	            }
	        } catch (FileNotFoundException e)
	        {
	            e.printStackTrace();
	        } catch (IOException e)
	        {
	            e.printStackTrace();
	        } finally
	        {
	            try
	            {
	                if (reader != null)
	                {
	                    reader.close();
	                }
	            } catch (IOException e)
	            {
	                e.printStackTrace();
	            }
	        }
	        
	        
	        String[] sequences = contents.toString().split(">seq[0-9]\r\n");
	        int[] n = new int[sequences.length];
	        int count = 0;
	        
	        for(int i = 0; i < sequences.length;i++){
	        	String[] parts = sequences[i].split("\r\n");
	        	String sq ="";
	        	for(int j = 0; j < parts.length; j++){
	        		sq+=parts[j];
	        	}
	        	if(!sq.isEmpty()){ n[i] = 1;count++;}
	        	
	        	sequences[i]=sq.toLowerCase().trim(); 
	        }	        
	        String[] finalSeqs = new String[count];
	        count = 0;
	        for(int i = 0; i < sequences.length;i++){
	        	if(n[i] == 1){
	        		finalSeqs[count] = sequences[i];
	        		count++;
	        	}
	        }       
	        
	        return finalSeqs;
	}
	
	public static void printOutput(String[] str){
		for(String dd: str){
			System.out.println(dd);
		}
	}
	
	
	public static String getSequenceFromFile(String fileLocation){
		File file = new File(fileLocation);
	    StringBuffer contents = new StringBuffer();
	    BufferedReader reader = null;

	    try
	      {
	            reader = new BufferedReader(new FileReader(file));
	            String text = null;

	            // repeat until all lines is read
	            while ((text = reader.readLine()) != null)
	            {
	            	if(!text.startsWith(">")){
	            		contents.append(text)
	                    		.append(System.getProperty(
	                    			"line.separator"));
	            	}
	            	
	            }
	        } catch (FileNotFoundException e)
	        {
	            e.printStackTrace();
	        } catch (IOException e)
	        {
	            e.printStackTrace();
	        } finally
	        {
	            try
	            {
	                if (reader != null)
	                {
	                    reader.close();
	                }
	            } catch (IOException e)
	            {
	                e.printStackTrace();
	            }
	        }
	        String[] seq_parts = contents.toString().split("\r\n");
	        String seq = "";
	        for(int i = 0; i < seq_parts.length;i++){
	        	seq = seq+seq_parts[i];
	        }
	        return seq.toLowerCase();
    }
	
	public static Hashtable<String,Hashtable<String,String>> readScoreMatrix(String fileLocation){
		File file = new File(fileLocation);
	    StringBuffer contents = new StringBuffer();
	    BufferedReader reader = null;

	    try
	      {
	            reader = new BufferedReader(new FileReader(file));
	            String text = null;

	            // repeat until all lines is read
	            while ((text = reader.readLine()) != null)
	            {
	            		contents.append(text)
	                    		.append(System.getProperty(
	                    			"line.separator"));	            
	            	
	           }
	        } catch (FileNotFoundException e)
	        {
	            e.printStackTrace();
	        } catch (IOException e)
	        {
	            e.printStackTrace();
	        } finally
	        {
	            try
	            {
	                if (reader != null)
	                {
	                    reader.close();
	                }
	            } catch (IOException e)
	            {
	                e.printStackTrace();
	            }
	        }
	        
	        Hashtable<String,Hashtable<String,String>> scoreMatrix = new Hashtable<String,Hashtable<String,String>>();
	        
	        String[] lines = contents.toString().split("\r\n");	        
	        int a = 0;
	        try {
	        	a = Integer.parseInt (""+lines[0].charAt(0));
	        } catch (Exception E){
	        	//es war keine Zahl
	        }	       
	        
	        String[] letters = new String[a];
	        
	        for(int i = 1; i <= a; i++){
	        	letters[i-1] = lines[i].substring(0,1).toLowerCase();	        	
	        }        
	        
	        for(int i = 0; i < a; i++){       		        	
	        	        	
	        	String index1 = letters[i];	        	 
	        	Hashtable<String,String> row = new Hashtable<String,String>();	        
	        	String[] list = lines[i+1].split("[ ]+");
	        	
	        	for(int j = 1; j < list.length; j++){
	        		
	        		String index2 = letters[j-1];	        		 
	        		row.put(index2,list[j]);	        		
		        	scoreMatrix.put(index1,row);
	        	}	        	
	        }
	        return scoreMatrix;
	        
	}	
	
}
